package com.deer.wms.base.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.deer.wms.base.system.dao.PackDetailMapper;
import com.deer.wms.base.system.model.Pack.PackDetail;
import com.deer.wms.base.system.model.Pack.PackDetail2;
import com.deer.wms.base.system.model.Pack.PackDetailCriteria;
import com.deer.wms.base.system.model.Pack.PackDetailDto;
import com.deer.wms.base.system.service.PackDetailService;
import com.deer.wms.project.root.service.SuperServiceImpl;
import com.deer.wms.project.root.util.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by guo on 2019/11/18.
 */
@Service
@Transactional
public class PackDetailServiceImpl extends SuperServiceImpl<PackDetailMapper, PackDetail> implements PackDetailService {

    @Autowired
    private PackDetailMapper packDetailMapper;

    @Autowired
    private RedisUtil redisUtil;

    public void update(PackDetail packDetail){
        super.updateById(packDetail);
        redisUtil.delKey("packCode"+packDetail.getPackCode());
    }


    @Override
    public List<PackDetailDto> findList(PackDetailCriteria criteria) {
        return packDetailMapper.findList(criteria);
    }

    @Override
    public List<PackDetailDto> findByPackId(Integer id) {
        return packDetailMapper.findByPackId(id);
    }

    @Override
    public void deleteByCode(String packCode) {
        packDetailMapper.deleteByCode(packCode);
    }


    @Override
    public PackDetail findLevel(Integer packDetailId) {
        return packDetailMapper.findLevel(packDetailId);
    }

    @Override
    public List<PackDetail> findByCode(String packCode) {
        return packDetailMapper.findByCode(packCode);
    }

    @Override
    public List<PackDetail2> findByCode2(String packCode) {
        if(packCode==null){
            packCode = "sys_pack";
        }
        List<PackDetail2> list = (List<PackDetail2>)redisUtil.get("packCode"+packCode);
        if(list==null){
            list = packDetailMapper.findByCode2(packCode);
            redisUtil.set("packCode"+packCode,list,20L);
        }
        return list;
    }

    @Override
    public PackDetail findBy(String field, String value) {
        QueryWrapper<PackDetail> qw = new QueryWrapper<>();
        return getOne(qw.eq(field, value));
    }

}
